import os,sys,commands


# 第一个入参（排序列）
sort_filed = sys.argv[1]
# 第二个入参（排序方式）
sort_type = sys.argv[2]
# 第三个入参（排序后展示行数）
line_count = int(sys.argv[3]) + 1

command = "ps -aux --sort=" + sort_type + sort_filed + " | head -n " + str(line_count) + " | awk \'{printf \"%8s %8s %8s %8s %8s %8s %8s %55s\\n\",$1,$2,$3,$4,$5,$6,$10,$11}\'"
print 'command:', command
(cmdstatus, output) = commands.getstatusoutput(command)
print 'result:', cmdstatus, '\n', output
print '=========================================='
total_rss = 0
first_line = True
for i in output.split('\n'):
    if first_line:
        first_line = False
        continue
    arr = i.split()
    total_rss = total_rss + int(arr[5])
print '总内存占用=%dM' % int(total_rss/1024)



# ps -aux --sort=+rss | head -n 6 | awk \{printf \"%8s %8s %8s %8s %8s %8s %8s %55s\\n\",$1,$2,$3,$4,$5,$6,$10,$11}\
